﻿using AutoMapper;
using MediatR;
using SmartCity.ProcessEngine.Api.Read.Application.Command.FormField;
using SmartCity.ProcessEngine.Api.Read.Dtos;
using SmartCity.ProcessEngine.Domain.Entities;
using SmartCity.ProcessEngine.ErrorCode;
using SmartCity.ProcessEngine.Infrastructure.Interface;

namespace SmartCity.ProcessEngine.Api.Read.Application.CommandHandler.FormField
{
    public class GetFormFieldHander : IRequestHandler<GetFormFieldCommand, ApiResult<List<FormFieldDto>>>
    {
        private readonly IBaseRepository<FormFieldModel> formFieldRepo;
        private readonly IMapper mapper;

        public GetFormFieldHander(IBaseRepository<FormFieldModel> formFieldRepo,IMapper mapper)
        {
            this.formFieldRepo = formFieldRepo;
            this.mapper = mapper;
        }

        /// <summary>
        /// 根据表单id 查询表单明细字段列表的中介者处理方法
        /// </summary>
        /// <param name="request">表单id</param>
        /// <param name="cancellationToken"></param>
        /// <returns> 查询表单明细字段列表</returns>
        public async Task<ApiResult<List<FormFieldDto>>> Handle(GetFormFieldCommand request, CancellationToken cancellationToken)
        {
            var fields = formFieldRepo.GetAll().Where(x => !x.IsDeleted&&x.FormId==request.FormId).ToList();
            var dtos = mapper.Map<List<FormFieldDto>>(fields);

            return new ApiResult<List<FormFieldDto>>().Success("查询表单字段明细成功",dtos);
        }
    }
}
